package com.dfe.backend.data;

import lombok.Data;

import java.util.HashMap;
import java.util.Map;

/**
 * @author 李畅
 */
@Data
public class RtnData {
    private String code = "000000";
    private String message = "success";
    private Object result;
    private String status = "OK";

    public RtnData() {
    }

    public static RtnData ok(Object result) {
        RtnData rtnData = new RtnData();
        rtnData.setCode("000000");
        rtnData.setStatus("OK");
        rtnData.setResult(result);
        return rtnData;
    }

    public static RtnData okMessage(String message) {
        RtnData rtnData = new RtnData();
        rtnData.setCode("000000");
        rtnData.setStatus("OK");
        rtnData.setMessage(message);
        return rtnData;
    }

    public static RtnData okPage(long totalCount, Object result) {
        RtnData rtnData = new RtnData();
        rtnData.setCode("000000");
        rtnData.setStatus("OK");
        Map<String, Object> params = new HashMap<>(2);
        params.put("totalCount", totalCount);
        params.put("items", result);
        rtnData.setResult(params);
        return rtnData;
    }

    public static RtnData fail(String message) {
        return fail(null, message);
    }

    public static RtnData fail(Object result, String message) {
        return fail(result, "999999", message);
    }

    public static RtnData fail(Object result, String code, String message) {
        RtnData rtnData = new RtnData();
        rtnData.setCode(code);
        rtnData.setMessage(message);
        rtnData.setStatus("ERROR");
        rtnData.setResult(result);
        return rtnData;
    }

}
